#include "WindowSDL_1_3.h"
#include "SystemSDL_1_3.h"

#include <SDL.h>

const int CWindowSDL_1_3::DEFAULT_WIDTH     = 640;
const int CWindowSDL_1_3::DEFAULT_HEIGHT    = 480;
const int CWindowSDL_1_3::DEFAULT_X_POS     = SDL_WINDOWPOS_UNDEFINED;
const int CWindowSDL_1_3::DEFAULT_Y_POS     = SDL_WINDOWPOS_UNDEFINED;
const char CWindowSDL_1_3::DEFAULT_TITLE[]  = "Default Title";
bool CWindowSDL_1_3::m_isVideoInitialized   = false;

CWindowSDL_1_3::CWindowSDL_1_3( void ) : m_Width( DEFAULT_WIDTH ), m_Height( DEFAULT_HEIGHT ),
                                         m_Xpos( DEFAULT_X_POS ), m_Ypos( DEFAULT_Y_POS ),
                                         m_CreateFlags( SDL_WINDOW_SHOWN ), m_Title( DEFAULT_TITLE ),
                                         m_hWnd( NULL ), m_hRenderer( NULL ), m_isQuitting( false )
{
    //ctor
}

CWindowSDL_1_3::~CWindowSDL_1_3( void )
{
    //dtor
}

int CWindowSDL_1_3::Init( void )
{
    // initialize SDL's video subsystem
    // but do so only once!
    if( m_isVideoInitialized == false )
    {
        if( SDL_VideoInit( NULL ) <  0 )
        {
            printf( "Could not initialize SDL's video subsystem\n" );
            printf( "SDL says: %s\n", SDL_GetError() );

            return -1;
        }
    }
    // create the window
    m_hWnd = SDL_CreateWindow( m_Title, m_Xpos, m_Ypos, m_Width, m_Height, m_CreateFlags );

    if( m_hWnd == NULL )
    {
        printf( "Could not create window at %dx%d\n", m_Width, m_Height );
        printf( "SDL says: %s", SDL_GetError() );

        return -1;
    }

    SDL_SetWindowData( m_hWnd, CSystemSDL_1_3_Main::WINDOW_PTR_NAME, this );

    m_hRenderer = SDL_CreateRenderer( m_hWnd, -1, 0 );

    if( m_hRenderer == NULL )
    {
        printf( "Could not create renderer, SDL says: %s\n", SDL_GetError() );
        return -1;
    }

    CSystemSDL_1_3_Renderer::m_hCurrentRenderer = m_hRenderer;

    SDL_SetRenderDrawColor( m_hRenderer, 0, 0, 255, 0 );
    SDL_RenderClear( m_hRenderer );
    CSystemSDL_1_3_Helper::PrintRenderer( m_hRenderer );

    return 0;
}

void CWindowSDL_1_3::Process( const SDL_Event& ev )
{
    CSystemSDL_1_3_Helper::PrintEvent( ev );

    if( ev.type == SDL_KEYUP )
    {
        if( ev.key.keysym.sym == SDLK_ESCAPE )
        {
            ShutDown();
        }
    }
}

void CWindowSDL_1_3::Clear( const unsigned char red, const unsigned char green,
                            const unsigned char blue, const unsigned char alpha )
{
    SDL_SetRenderDrawColor( m_hRenderer, red, green, blue, alpha );
    SDL_RenderClear( m_hRenderer );
}

void CWindowSDL_1_3::Clear( void )
{
    SDL_RenderClear( m_hRenderer );
}

void CWindowSDL_1_3::Refresh( void )
{
    SDL_RenderPresent( m_hRenderer );
}

void CWindowSDL_1_3::ShutDown( void )
{
    m_isQuitting = true;
    CSystemSDL_1_3_Main::Quit();

    if( m_hRenderer != NULL )
    {
        SDL_DestroyRenderer( m_hRenderer );
        m_hRenderer = NULL;
    }

    if( m_hWnd != NULL )
    {
        SDL_DestroyWindow( m_hWnd );
        m_hWnd = NULL;
    }

    if( m_isVideoInitialized == true )
        SDL_VideoQuit();
}

void CWindowSDL_1_3::SetTitle( const char* title )
{
    if( m_hWnd != NULL )
    {
        SDL_SetWindowTitle( m_hWnd, title );
        m_Title = SDL_GetWindowTitle( m_hWnd );
    }
}
